package com.wbt.bambootown.mvp.model;


import com.google.gson.Gson;
import com.wbt.bambootown.BuildConfig;
import com.wbt.bambootown.FrameApp;
import com.wbt.bambootown.cons.Cons;
import com.wbt.bambootown.entity.response.LoginResponse;
import com.wbt.bambootown.mvp.frame.MvpModel;
import com.wbt.bambootown.okhttp.OkHttpUtils;
import com.wbt.bambootown.okhttp.builder.PostFormBuilder;
import com.wbt.bambootown.utils.Log;
import com.wbt.bambootown.utils.SPUtils;

import java.io.IOException;

import me.jessyan.retrofiturlmanager.RetrofitUrlManager;
import okhttp3.Response;
import rx.Observable;
import rx.Subscriber;

public class LoginModel extends MvpModel {
   // private static final String POST_URL = SPUtils.getString(Cons.SPCons.API_ADDRESS)   + "/api/AppData/" + "Login";


    public Observable<LoginResponse> login(
            final String uid,
            final String pwd
    ) {
        return
                Observable.create(new Observable.OnSubscribe<LoginResponse>() {
                    @Override
                    public void call(Subscriber<? super LoginResponse> subscriber) {
                        String POST_URL = SPUtils.getString(Cons.SPCons.API_ADDRESS)   + "/api/AppData/" + "Login";
                        PostFormBuilder formBuilder = OkHttpUtils.post().url(POST_URL);

                        formBuilder.addParams("uid", uid);
                        formBuilder.addParams("pwd", pwd);

                        try {
                            Response response = formBuilder.build().execute();
                            String str = response.body().string();
                            LoginResponse postResponse = new Gson().fromJson(str, LoginResponse.class);
                            subscriber.onNext(postResponse);
                            subscriber.onCompleted();
                        } catch (IOException e) {
                            e.printStackTrace();
                            subscriber.onError(e);
                        }
                    }
                });
    }


}
